package io.renren.modules.store.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.common.utils.DateUtils;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreStaffEntity;
import io.renren.modules.store.service.TStoreStaffService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-22 23:35:07
 */
@RestController
@RequestMapping("store/tstorestaff")
public class TStoreStaffController extends AbstractController {
    @Autowired
    private TStoreStaffService tStoreStaffService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorestaff:list")
    public R list(@RequestParam Map<String, Object> params){
        if (getSeller() != null){
            params.put("sellerId",getSeller());
        }
        PageUtils page = tStoreStaffService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 分配角色
     */
    @RequestMapping("/distributionRole")
    @RequiresPermissions("store:tstorestaff:distributionRole")
    public R distributionRole(@RequestBody TStoreStaffEntity staffEntity){
        sysUserRoleService.saveOrUpdate(staffEntity.getUserId(),staffEntity.getRoleIdList());
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{staffId}")
    @RequiresPermissions("store:tstorestaff:info")
    public R info(@PathVariable("staffId") Long staffId){
		TStoreStaffEntity tStoreStaff = tStoreStaffService.getById(staffId);

        return R.ok().put("tStoreStaff", tStoreStaff);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorestaff:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TStoreStaffEntity tStoreStaff){
        TStoreStaffEntity one = tStoreStaffService.getOne(new QueryWrapper<TStoreStaffEntity>()
                .eq("seller_id", getSeller())
                .eq("staff_no", tStoreStaff.getStaffNo())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("员工编号已存在，请重新设置");
        }
        //插入用户表
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(tStoreStaff.getStaffTel());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        sysUserEntity.setPassword(new Sha256Hash("123456", salt).toHex());
        sysUserEntity.setSalt(salt);
        sysUserEntity.setMobile(tStoreStaff.getStaffTel());
        sysUserEntity.setCreateUserId(getUserId());
        sysUserEntity.setStatus(1);
        sysUserEntity.setSellerId(getSeller());
        sysUserService.save(sysUserEntity);

        //插入后获取userId
        SysUserEntity userEntity = sysUserService.queryByUserName(tStoreStaff.getStaffTel());

        //插入员工表
        tStoreStaff.setUserId(userEntity.getUserId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String s = simpleDateFormat.format(tStoreStaff.getStaffBirthday());
        tStoreStaff.setStaffBirthday(DateUtils.stringToDate(s,DateUtils.DATE_PATTERN));
        tStoreStaff.setSellerId(getSeller());
        tStoreStaff.setCreateUserId(getUserId());
        tStoreStaff.setCreateTime(new Date());
        tStoreStaff.setEntryTime(new Date());
        tStoreStaff.setStatus(1);
        tStoreStaffService.save(tStoreStaff);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorestaff:update")
    public R update(@RequestBody TStoreStaffEntity tStoreStaff){
		tStoreStaffService.updateById(tStoreStaff);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorestaff:delete")
    public R delete(@RequestBody Long[] staffIds){
		tStoreStaffService.removeByIds(Arrays.asList(staffIds));

        return R.ok();
    }

}
